package com.example.tetrislogin;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;


import com.example.tetrislogin.databinding.ActivityMainBinding;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

public class MainActivity extends AppCompatActivity {
    public static String phoneNumber;
    ActivityMainBinding mBinding;
    private Boolean isChecked = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        EdgeToEdge.enable(this);
        setContentView(mBinding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        mBinding.gainCode.setOnClickListener(v -> {
            if (mBinding.etPhone.getText().length()==11){
                if (isChecked){
                    BmobSMS.requestSMSCode(mBinding.etPhone.getText().toString().trim(), "DataSDK", new QueryListener<Integer>() {
                        @Override
                        public void done(Integer smsId, BmobException e) {
                            if (e == null) {
                                phoneNumber=mBinding.etPhone.getText().toString().trim();
                                Intent intent = new Intent(MainActivity.this,VerifyActivity.class);
                                startActivity(intent);
                            } else {
                                Toast.makeText(MainActivity.this, "请输入有效的手机号", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }else {
                    Toast.makeText(MainActivity.this, "请阅读并同意协议", Toast.LENGTH_SHORT).show();
                }
            }else if (mBinding.etPhone.getText().length()==0){
                Toast.makeText(MainActivity.this, "手机号为空", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this, "请输入正确位数的手机号", Toast.LENGTH_SHORT).show();
            }
        });
        mBinding.ivCheck.setOnClickListener(v -> {
            isChecked = !isChecked;
            if (isChecked){
                mBinding.ivCheck.setImageResource(R.drawable.ic_selected_circle);
            }else {
                mBinding.ivCheck.setImageResource(R.drawable.ic_check_circle);
            }
        });
    }
}